package com.znsd.zl.browse.controller;

import com.znsd.zl.browse.entity.TBrowseEntity;
import com.znsd.zl.browse.entity.vo.TBrowseEntityVo;
import com.znsd.zl.browse.entity.vo.THouseVo;
import com.znsd.zl.browse.service.TBrowseService;
import com.znsd.zl.browse.service.THouseService;
import com.znsd.zl.utils.BaseController;
import com.znsd.zl.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @ClassName TBrowse
 * @Description 描述
 * @author xyx
 * @date 2020-08-22 15:18:00
 * @Version v1.0
 */
@RestController
@RequestMapping("/zl/browse")
public class BrowseController extends BaseController<TBrowseService, TBrowseEntity, Integer> {

	@Autowired
	THouseService tHouseService;

	@PostMapping("/browseSave")
	public R browseSave(@RequestBody TBrowseEntity entity){
		System.out.println("browseSave"+entity.toString());
		try {
			/*判断登录的用户Id是否为null，不存在则为未登录游客不进行添加*/
			if(entity.getAppUserId()!=null){
				return service.browseSave(entity) ? R.ok() : R.error("添加失败");
			}else{
				System.out.println("游客登录");
				return R.error();
			}
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	@GetMapping("/list")
	public R list(Integer userId) {
		try {
			List<TBrowseEntityVo> list = null;
			list = service.findByBrowseId(userId);
			return R.ok().put("list",list);
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	@GetMapping("/findByAll")
	public R findByAll(Integer id){
		try {
			R byHouse = service.findByHouse(id);
			return byHouse;
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	@GetMapping("/test")
	public R test(Integer id){
		try {
			R byHouse = service.findByHouse(id);
			return byHouse;
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

	@GetMapping("/findHouse")
	public R findHouse(Integer houseId){
		try {
			System.out.println(houseId);
			List<THouseVo> house = tHouseService.findHouse(houseId);
			return R.ok().put("list",house);
		}catch (Exception e){
			e.printStackTrace();
			return R.error();
		}
	}

}
